// pages/user/user.js
const app = getApp()
import { Register } from 'register-model.js'
import { hex_md5 } from '../../utils/md5.js'
var register = new Register();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    timer: 59,
    phone: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  setPhone(e) {
    let val = e.detail.value
    this.setData({
      phone: val
    })
  },
  checkPhone(e) {
    console.log(e)
    let val = e.detail.value

    if (val == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号'
      })
    } else if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(val)) {
      wx.showToast({
        icon: 'none',
        title: '请输入正确的手机号'
      })
    }
  },
  setPassword(e) {
    let val = e.detail.value
    this.setData({
      password: val
    })
  },
  checkPassword(e) {
    console.log(e)
    let val = e.detail.value

    if (val == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入密码'
      })
    } else if (!/^[\w]{6,12}$/.test(val)) {
      wx.showToast({
        icon: 'none',
        title: '请输入6-20位数字、字母和下划线'
      })
    }
  },
  /*
  *获取验证码
  */
  getCode() {
    console.log(this.data.phone)
    let { phone } = this.data

    if (phone == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号'
      })
      return;
    }
    let timer = null


    let time = 59
    console.log(/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(phone), phone.length)
    console.log('send')
    register.checkVerifPhone({ verifyPhone: phone }, res => {
      console.log('code', res)
      if (!res.status) {
              console.log('send fail')
        wx.showToast({
          icon: 'none',
          title: res.message,
          duration:2000
        })
      } else {
        register.sendVerifyCode({ verifyPhone: phone }, res => {
          wx.hideLoading()
          if (!res.status) {
            wx.showToast({
              icon: 'none',
              title: res.message
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: '发送验证码成功，请您查收'
            })
            this.setData({
              disabled: true
            })
            timer = setInterval(() => {

              if (time == 0) {
                clearInterval(timer)
                this.setData({
                  disabled: false,
                  timer: 59
                })
              } else {
                this.setData({
                  timer: time
                })
                time--
              }

            }, 1000)
          }
        })

      }
    })
  },
  /*
  *注册
  */
  onRegister(e) {
    console.log(e);
    let formData = e.detail.value;

    let name = formData.verifyPhone;
    let yzm = formData.verifyCode;
    let yqm = formData.inviteCode;
    let password = formData.password;
    if (name == "") {
      wx.showToast({
        icon: 'none',
        title: '手机号不能为空'
      })
      return;
    }
    if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(name)) {
      wx.showToast({
        icon: 'none',
        title: '请输入正确的手机号'
      })
      return;
    }
    if (yzm.trim() == "") {
      wx.showToast({
        icon: 'none',
        title: '验证码不能为空'
      })
      return;
    }
    if (!/\d{6}/g.test(yzm)) {
      wx.showToast({
        icon: 'none',
        title: '验证码为6位的数字'
      })
      return;
    }
    if (password == "") {
      wx.showToast({
        icon: 'none',
        title: '密码不能为空'
      })
      return;
    }
    if (!/^[\w]{6,12}$/.test(password)) {
      wx.showToast({
        icon: 'none',
        title: '请输入6-20位数字、字母和下划线'
      })
      return;
    }

    if (yqm.trim() == "") {
      wx.showToast({
        icon: 'none',
        title: '邀请码不能为空'
      })
      return;
    }
    if (!/^\w{6}$/g.test(yqm)) {
      wx.showToast({
        icon: 'none',
        title: '邀请码为6位'
      })
      return;
    }


    formData.password = hex_md5(formData.password);
    let parems = formData;
    console.log("parems--------------", parems);
    register.registerUser(parems, res => {
      if (!res.status) {
        wx.showToast({
          icon: 'none',
          title: res.message
        })
      } else {
        wx.showToast({
          icon: 'none',
          title: "注册成功!"
        })
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/signin/signin'
          })
        }, 300)
      }
    })

  }
})